namespace Peer.Web.Controls.Upload
{
    using System;
    using System.IO;
    using System.Text;
    using System.Web;

    internal class WebbHelper
    {
        private static string RESOURCE_FILE_PREFIX = "Controls.Upload.Resources.";
        private static string version;

        public static HttpContext GetContext()
        {
            HttpContext current = HttpContext.Current;
            if (current == null)
            {
                throw new Exception("HttpContext not found");
            }
            return current;
        }

        public static string GetFormatString(double size)
        {
            if (size >= 1048576.0)
            {
                return (Math.Round((double) (size / 1048576.0), 2) + " MB");
            }
            if (size >= 1024.0)
            {
                return (Math.Round((double) (size / 1024.0), 2) + " KB");
            }
            return (size + " B");
        }

        public static string GetFormatString(TimeSpan span)
        {
            string str = string.Empty;
            if ((span.Days > 0) || (span.Hours > 0))
            {
                int num = (0x18 * span.Days) + span.Hours;
                str = str + num + "&nbsp;Hour(s)&nbsp;";
            }
            if (span.Minutes > 0)
            {
                str = str + span.Minutes + "&nbsp;Minute(s)&nbsp;";
            }
            if (span.Seconds > 0)
            {
                str = str + span.Seconds + "&nbsp;Second(s)&nbsp;";
            }
            return str;
        }

        public static StringBuilder GetHtml(string filename)
        {
            byte[] bytes = LoadFileFromCache(filename);
            if (bytes == null)
            {
                throw new ArgumentNullException("filename", "isn't find " + filename);
            }
            if (bytes.Length == 0)
            {
                throw new ArgumentNullException("filename", "isn't find " + filename);
            }
            return new StringBuilder(Encoding.Default.GetString(bytes));
        }

        public static string GetUploadFolder()
        {
            string path = GetContext().Request["Webb_Upload_TempPath"];
            if (((path != null) && !(path == string.Empty)) && Directory.Exists(path))
            {
                return path;
            }
            return Path.GetTempPath();
        }

        public static string GetVersion()
        {
            if (version == null)
            {
                version = typeof(WebbHelper).Assembly.GetName().Version.Major.ToString();
            }
            return version;
        }

        public static bool IsAccordantBrowser()
        {
            HttpBrowserCapabilities browser = GetContext().Request.Browser;
            return (!(browser.Browser != "IE") && (float.Parse(browser.Version) >= 5.5));
        }

        public static byte[] LoadAssemblyFiles(string i_filename)
        {
            if (i_filename == null)
            {
                throw new ArgumentNullException("i_filename");
            }
            string name = RESOURCE_FILE_PREFIX + i_filename;
            Stream manifestResourceStream = typeof(WebbHelper).Assembly.GetManifestResourceStream(name);
            byte[] buffer = new byte[manifestResourceStream.Length];
            manifestResourceStream.Read(buffer, 0, buffer.Length);
            manifestResourceStream.Close();
            return buffer;
        }

        private static byte[] LoadFileFromCache(string i_filename)
        {
            HttpContext context = GetContext();
            if (context.Cache[RESOURCE_FILE_PREFIX + i_filename] == null)
            {
                context.Cache[RESOURCE_FILE_PREFIX + i_filename] = LoadAssemblyFiles(i_filename);
            }
            return (byte[]) context.Cache[RESOURCE_FILE_PREFIX + i_filename];
        }
    }
}

